﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for DoKhoDAL
/// </summary>
public class DoKhoDAL
{
    public static object IsNull(object checkValue, object replaceValue)
    {
        if (checkValue.Equals(System.DBNull.Value) || checkValue == null)
            return replaceValue;
        return checkValue;
    }

    public static List<DoKhoINFO> listAll()
    {
        string sql = "select * from DoKho";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetListObjects<DoKhoINFO>(dr);
    }

    public static DoKhoINFO GetInfo(int madokho)
    {
        string sql = "select * from LoaiCauHoi where madokho =" + madokho + "";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetObject<DoKhoINFO>(dr);
    }

    public static int Insert(DoKhoINFO obj)
    {
        obj.MaDoKho = GetMa();
        obj.MaDoKho = obj.MaDoKho + 1;
        string sql = "INSERT INTO DOKHO (MaDoKho,DoKho) VALUES (" + obj.MaDoKho + ",'" + obj.DoKho + "')";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }

    public static int Update(DoKhoINFO obj)
    {
        string sql = "Update DoKho set DoKho='" + obj.DoKho + "' Where MaDoKho =" + obj.MaDoKho + "";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }

    public static int Delete(int madokho)
    {
        string sql = "Delete from DoKho where madokho =" + madokho + "";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }

    public static int GetMa()
    {
        string sql = "SELECT ISNULL(MAX(MaLoai),0) FROM DOKHO";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        int ma = 0;
        if (dr.Read())
        {
            ma = int.Parse(dr[0].ToString());
        }
        dr.Close();
        return ma;
    }
}